﻿#region License

// Another free, open solution by folks who get it.
// Copyright © 2011 Business in Unison, Inc.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Patterns.Text
{
	/// <summary>
	/// 	Defines extensions for strings.
	/// </summary>
	public static class StringExtensions
	{
		/// <summary>
		/// Gets or sets the inflection engine.
		/// </summary>
		/// <value>The inflection engine.</value>
		public static IInflector InflectionEngine { get; set; }

		static StringExtensions()
		{
			InflectionEngine = new Inflector();
		}

		/// <summary>
		/// 	Joins the specified strings.
		/// </summary>
		/// <param name = "items">The items.</param>
		/// <param name = "separatorAction">The separator action.</param>
		/// <returns></returns>
		public static string JoinStrings(this IEnumerable<string> items, Action<StringBuilder> separatorAction)
		{
			var builder = new StringBuilder();
			if (separatorAction != null) separatorAction(builder);
			string separator = builder.ToString();
			return string.Join(separator, items.ToArray());
		}

		/// <summary>
		/// 	Gets the lines of text that make up the current string.
		/// </summary>
		/// <param name = "text">The text.</param>
		/// <returns></returns>
		public static IEnumerable<string> GetLines(this string text)
		{
			using (var reader = new StringReader(text))
			{
				string line;
				while ((line = reader.ReadLine()) != null)
					yield return line;
			}
		}

		/// <summary>
		/// Determines whether the text matches the specified pattern.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="pattern">The pattern.</param>
		/// <returns>
		/// 	<c>true</c> the text matches the specified pattern; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsPatternMatch(this string text, CompiledRegex pattern)
		{
			return pattern.IsMatch(text);
		}

		/// <summary>
		/// Determines whether the current text is equivalent to the specified text.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="compareTo">The compare to.</param>
		/// <param name="ignorePluralization">If set to <c>true</c> ignore
		/// pluralization. Treated as <c>false</c> whenever <paramref name="text"/>
		/// contains more than one word.</param>
		/// <returns>
		/// 	<c>true</c> if the current text is equivalent to the specified text;
		/// 	otherwise, <c>false</c>.
		/// </returns>
		public static bool IsEquivalentTo(this string text, string compareTo, bool ignorePluralization = false)
		{
			bool normalizePluralization = ignorePluralization && !text.IsPatternMatch(@"\s+");

			text = normalizePluralization ? InflectionEngine.GetSingularForm(text) : text;
			compareTo = normalizePluralization ? InflectionEngine.GetSingularForm(compareTo) : compareTo;

			return StringComparer.OrdinalIgnoreCase.Equals(text, compareTo);
		}
	}
}